/* eslint-disable */
function _ignore(ignore) {
  var ignoreType, prefix;

  ignoreType = ui.core.type(ignore);
  if (ignoreType !== 'function') {
    if (ignoreType === 'string') {
      prefix = ignore;
      ignore = function() {
        return index.indexOf(prefix) > -1;
      };
    } else {
      ignore = function() {
        return this.indexOf('_') > -1;
      };
    }
  }
  return ignore;
}

export class ObjectHelper {
  static clone(source, ignore) {
    let result, type, key;
    ignore = _ignore(ignore);
    type = ui.core.type(source);

    if (type === 'object') {
      result = {};
    } else if (type === 'array') {
      result = [];
    } else {
      return source;
    }

    for (key in source) {
      if (ignore.call(key)) {
        continue;
      }
      result[key] = source[key];
    }
    return result;
  }

  static deepClone(source, ignore) {
    let result, type, copy, key;

    type = ui.core.type(source);
    if (type === 'object') {
      result = {};
    } else if (type === 'array') {
      result = [];
    } else {
      return source;
    }

    ignore = _ignore(ignore);
    for (key in source) {
      if (ignore.call(key)) {
        continue;
      }
      copy = source[key];
      if (result === copy) continue;
      type = ui.core.type(copy);
      if ((type === 'object' && ui.core.isPlainObject(copy)) || type === 'array') {
        result[key] = this.deepClone(copy, ignore);
      } else {
        result[key] = copy;
      }
    }
    return result;
  }
}
